﻿@model Telerik.Web.Mvc.GridModel<CurrencyModel>
@using Telerik.Web.Mvc.UI
@using SmartStore.Core.Domain.Directory;
@{
	ViewBag.Title = T("Admin.Configuration.Currencies").Text;

	List<ExchangeRate> rates = ViewBag.Rates;
}

<div class="section-header">
    <div class="title">
        <i class="far fa-money-bill-alt"></i>
        @T("Admin.Configuration.Currencies")
    </div>
    <div class="options">
        <a href="@Url.Action("Create")" class="btn btn-primary">
            <i class="fa fa-plus"></i>
            <span>@T("Admin.Common.AddNew")</span>
        </a>
        <a href="javascript:document.CurrencySettings.submit()" class="btn btn-warning">
            <i class="fa fa-check"></i>
            <span>@T("Admin.Common.Save")</span>
        </a>
        <a href="@Url.Action("List", "Currency", new { liveRates = true })" class="btn btn-secondary">
            <i class="fa fa-sync"></i>
            <span>@T("Admin.Configuration.Currencies.GetLiveRates")</span>
        </a>
    </div>
</div>

@using (Html.BeginForm("Save", "Currency", FormMethod.Post, new { name = "CurrencySettings" }))
{
	@(Html.SmartStore().TabStrip().Name("currency-tabs").Style(TabsStyle.Material).Items(x =>
	{
		x.Add().Text(T("Admin.Configuration.Currencies").Text).Content(TabList()).Selected(true);

		if (rates != null) 
		{
			x.Add().Text(T("Admin.Configuration.Currencies.LiveRates").Text).Content(TabLiveRates());	
		}

		x.Add().Text(T("Common.Options").Text).Content(TabOptions());
		
		//generate an event
		EngineContext.Current.Resolve<IEventPublisher>().Publish(new TabStripCreated(x, "currency-config", this.Html, this.Model));
	}))

}

@helper TabOptions()
{
	bool autoUpdateEnabled = ViewBag.AutoUpdateEnabled ?? false;

    <table class="adminContent">
        <tr>
            <td class="adminTitle">
				<div class="ctl-label">
					<label>@T("Admin.Configuration.Currencies.Fields.ExchangeRateProvider")</label>
				</div>
            </td>
            <td class="adminData">
                @Html.DropDownList("exchangeRateProvider", ViewBag.ExchangeRateProviders as IEnumerable<SelectListItem>)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
				<div class="ctl-label">
					<label>@T("Admin.Configuration.Currencies.Fields.CurrencyRateAutoUpdateEnabled")</label>
				</div>
            </td>
            <td class="adminData">
                @Html.CheckBox("autoUpdateEnabled", autoUpdateEnabled)
            </td>
        </tr>
    </table>
}

@helper TabList()
{
	<div>
		@(Html.Telerik().Grid<CurrencyModel>()
				.Name("currencies-grid")
				.BindTo(Model.Data)
				.Columns(columns =>
				{
					columns.Bound(x => x.Name)
						.Template(x => Html.LabeledCurrencyName(x.Id, x.Name, x.IsPrimaryStoreCurrency, x.IsPrimaryExchangeRateCurrency));
					columns.Bound(x => x.CurrencyCode)
						.Centered()
						.Width(160);
					columns.Bound(x => x.Published)
						.Template(item => Html.SymbolForBool(item.Published))
						.Centered()
						.Width(160);
					columns.Bound(x => x.Rate)
						.RightAlign()
						.Format("{0:0.00000}")
						.Width(160);
					columns.Bound(x => x.LimitedToStores)
						.Template(item => Html.SymbolForBool(item.LimitedToStores))
						.Centered()
						.Width(180);
					columns.Bound(x => x.DomainEndings);
				})
		)
	</div>
}

@helper TabLiveRates()
{
	List<ExchangeRate> rates = ViewBag.Rates;
	
	if (rates != null)
	{
		<div>
			@{
				Func<string, string> headerTemplate = (caption) => "<span class=\"t-link\">{0}</span>".FormatInvariant(caption);
				Func<ExchangeRate, object, string> rowTemplate = (rate, value) => "<span class='{0}'>{1}</span>".FormatInvariant(rate.IsStoreCurrency ? "" : "muted", value);
			}

			@(Html.Telerik().Grid<ExchangeRate>()
				.Name("rate-grid")
				.DataKeys(x =>
				{
					x.Add(y => y.CurrencyCode).RouteKey("CurrencyCode");
				})
				.BindTo(rates.OrderBy(x => !x.IsStoreCurrency).ThenBy(x => x.Name))
				.Columns(columns =>
				{
					columns.Bound(x => x.Name)
						.HeaderTemplate(headerTemplate(T("Admin.Configuration.Currencies.Fields.Name")))
						.Template(x => rowTemplate(x, x.Name));
					columns.Bound(x => x.CurrencyCode)
						.Centered()
						.HeaderTemplate(headerTemplate(T("Admin.Configuration.Currencies.Fields.CurrencyCode")))
						.Template(x => rowTemplate(x, x.CurrencyCode));
					columns.Bound(x => x.UpdatedOn)
						.Centered()
						.HeaderTemplate(headerTemplate(T("Admin.Common.Updated")))
						.Template(x => rowTemplate(x, x.UpdatedOn.RelativeFormat(true, "f")));
					columns.Bound(x => x.Rate)
						.RightAlign()
						.HeaderTemplate(headerTemplate(T("Admin.Configuration.Currencies.Fields.Rate")))
						.Template(x => rowTemplate(x, x.Rate.ToString("0.00000")));
					columns.Bound(x => x)
						.Centered()
						.Filterable(false)
						.HeaderTemplate(headerTemplate(T("Admin.Configuration.Currencies.ApplyRate")))
						.Template(x => Html.ActionLink(T("Admin.Configuration.Currencies.ApplyRate").Text, "ApplyRate",
							new { rate = x.Rate, currencyCode = x.CurrencyCode },
							new { @class = x.IsStoreCurrency ? "t-button t-grid-edit" : "hide" }))
						.HtmlAttributes(new { align = "right", @class = "omega" });
				})
				.EnableCustomBinding(true)
			)
		</div>
	}
}